package com.bosssoft.cloud.wechat.core.api.impl;

import cn.hutool.core.map.MapUtil;
import com.bosssoft.cloud.wechat.core.api.SignatureService;
import com.bosssoft.cloud.wechat.core.api.TemplateService;
import com.bosssoft.cloud.wechat.core.bean.template.SendInvoiceSuccessParam;
import com.bosssoft.cloud.wechat.core.bean.template.TemplateDataInfo;
import com.bosssoft.cloud.wechat.core.bean.template.WxTemplate;
import com.bosssoft.cloud.wechat.core.bean.template.WxTemplateDataEnum;
import com.bosssoft.cloud.wechat.core.bean.template.WxTemplateIndustry;
import com.bosssoft.cloud.wechat.core.bean.WxApiBaseR;
import com.bosssoft.cloud.wechat.core.infra.config.WxTemplet;
import com.bosssoft.cloud.wechat.core.infra.enums.WxApiUrl;
import com.bosssoft.cloud.wechat.core.infra.properties.WxTempletProperties.SendTemplate;
import com.bosssoft.cloud.wechat.infrastructure.client.OkClient;
import com.bosssoft.cloud.wechat.infrastructure.enums.ErrEnum;
import com.bosssoft.cloud.wechat.infrastructure.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

import static com.bosssoft.cloud.wechat.infrastructure.constant.WxKeywordConsts.WX_RESULT_SUCCESS;

/**
 * @author wcy create on 2021/7/1 14:45 下午
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    private SignatureService signatureService;
    @Resource
    private OkClient okClient;

    @Override
    public void setIndustry(String industry_id1, String industry_id2) {
        Map<Object, Object> map = MapUtil.builder()
                .put("industry_id1", industry_id1)
                .put("industry_id2", industry_id2)
                .build();
        WxApiBaseR res = okClient.newClient(WxApiBaseR.class)
                .runWx(map, WxApiUrl.Template.TEMPLATE_API_SET_INDUSTRY, signatureService.getAccessToken())
                .execute();
        Assert.isTrue(res.getErrcode().equals(WX_RESULT_SUCCESS), ErrEnum.C0632, res.toString());
    }

    @Override
    public WxTemplateIndustry getIndustry() {
        return null;
    }

    @Override
    public WxTemplate getAllPrivateTemplate() {
        return null;
    }

    @Override
    public Boolean delPrivateTemplate(String template_id) {
        return null;
    }

    @Override
    public void sendInvoiceSuccessNotice(SendInvoiceSuccessParam param) {
        TemplateDataInfo data = new TemplateDataInfo();
        // 根据开票成功通知所属的sendId，获取开票成功通知模板消息的基础信息
        SendTemplate.SendData send = WxTemplet.getSendBySendId(WxTemplateDataEnum.INVOICE_SUCCESS_NOTICE.getSendId());
        if (send == null) throw new NullPointerException("模板消息配置有误，请联系马虎的开发者！");
        data.setTouser(param.getTouser());
        data.setTemplate_id(send.getTemplateId());
        data.setUrl(send.getUrl());
        // 将模板消息的主体信息（first,keyword1...）放入data
        for (SendTemplate.SendMsgInfo datum : send.getMsgInfo()) {
            data.putItem(datum.getKey(), String.format(datum.getValue(), param.getByKeyword(datum.getKey())), datum.getColor());
        }
        WxApiBaseR res = okClient.newClient(WxApiBaseR.class)
                .runWx(data, WxApiUrl.Template.MESSAGE_TEMPLATE_SEND, signatureService.getAccessToken())
                .execute();
        Assert.isTrue(res.getErrcode().equals(WX_RESULT_SUCCESS), ErrEnum.C0631, res.toString());
    }
}
